/*
 * UnsavedChangesDialog.java
 *
 * Created on December 24, 2008, 10:48 AM
 *
 * Copyright 2008 David D. Emory
 *
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 *
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.admin.gui.dialogs;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import org.fpdev.apps.admin.AdminClient;
import org.fpdev.apps.admin.gui.ACGUI;
import org.fpdev.util.gui.GUIFactory;

/**
 *
 * @author demory
 */
public class UnsavedChangesDialog extends JDialog implements ActionListener {

  public JButton saveBtn_, closeBtn_, cancelBtn_;


  private Selection selection_;

  public enum Selection { SAVE_AND_CLOSE, CLOSE_WITHOUT_SAVING, CANCEL }

  public UnsavedChangesDialog(ACGUI gui, List<AdminClient.UnsavedChange> changes) {
    super(gui, "Unsaved Changes", true);

    String text = "";
    for(AdminClient.UnsavedChange change : changes) {
      text += change.toString() + "\n";
    }
    JTextArea textArea = new JTextArea(text);
    JPanel textPanel = new JPanel(new GridLayout(1,1));
    textPanel.add(new JScrollPane(textArea));
    textPanel.setBorder(new EmptyBorder(8,0,8,0));

    saveBtn_ = GUIFactory.newButton("Save and Close", 120, this);
    closeBtn_ = GUIFactory.newButton("Close without Saving", 120, this);
    cancelBtn_ = GUIFactory.newButton("Cancel", 60, this);
    JPanel buttonRow = GUIFactory.newRowPanel();
    buttonRow.add(Box.createHorizontalGlue());
    buttonRow.add(saveBtn_);
    buttonRow.add(Box.createHorizontalStrut(5));
    buttonRow.add(closeBtn_);
    buttonRow.add(Box.createHorizontalStrut(5));
    buttonRow.add(cancelBtn_);
    buttonRow.add(Box.createHorizontalGlue());


    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(GUIFactory.newLabel("Warning: The following changes have not been saved:"), BorderLayout.NORTH);
    mainPanel.add(textPanel, BorderLayout.CENTER);
    mainPanel.add(buttonRow, BorderLayout.SOUTH);
    mainPanel.setBorder(new EmptyBorder(8,8,8,8));

    this.getContentPane().add(mainPanel);
    this.pack();
    this.setLocationRelativeTo(null);
    this.setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == saveBtn_) selection_ = Selection.SAVE_AND_CLOSE;
    if(e.getSource() == closeBtn_) selection_ = Selection.CLOSE_WITHOUT_SAVING;
    if(e.getSource() == cancelBtn_) selection_ = Selection.CANCEL;
    this.setVisible(false);
  }

  public Selection getSelection() { return selection_; }


}
